import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import path from "path";
// https://vite.dev/config/
export default defineConfig({
  build: {
    chunkSizeWarningLimit: 2000,
    rollupOptions: {
      output: {
        entryFileNames: "js/[name]-[hash].js",
        chunkFileNames: "js/chunks/[name]-[hash].js",
        manualChunks: {
          // 把 react 全家桶单独打包
          react: ["react", "react-dom", "react-router-dom"],
          // UI 库单独打包，比如 antd 或 arco
          arco: ["@arco-design/web-react"],
          antd: ["antd"],
          // 其他依赖可以继续拆
          lodash: ["lodash"],
          echarts: ["echarts"],
        },
        assetFileNames: ({ name }) => {
          if (/\.(css)$/.test(name ?? "")) {
            return "css/[name]-[hash][extname]";
          }
          if (/\.(png|jpg|jpeg|gif|svg|webp)$/.test(name ?? "")) {
            return "images/[name]-[hash][extname]";
          }
          if (/\.(woff2?|eot|ttf|otf)$/.test(name ?? "")) {
            return "fonts/[name]-[hash][extname]";
          }
          return "assets/[name]-[hash][extname]";
        },
      },
    },
  },
  plugins: [react()],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
});
